package logindemo.demo.dao;

import logindemo.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public User selectOne(String username,String password){
        String sql = "select * from user where username = '" + username + "' and password = '" + password + "'";
        User user = null;
        try {
            user = jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
                @Override
                public User mapRow(ResultSet resultSet, int i) throws SQLException {
                    User user = null;
                    if(resultSet != null){
                        user = new User();
                        user.setId(resultSet.getInt("id"));
                        user.setUsername(resultSet.getString("username"));
                        user.setPassword(resultSet.getString("password"));
                    }
                    return user;
                }
            });
        } catch (DataAccessException e) {
        }
        return user;
    }
}
